package com.qianchang.microfinance.view.dialog;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.bigkoo.pickerview.TimePickerView;
import com.qianchang.microfinance.R;

import java.util.Calendar;
import java.util.Date;

/**
 * 版 权: 深圳市迪蒙网络科技有限公司
 * 描 述: <日期选择>
 * 修 改 人: sushuo
 * 修改时间: 2017-3-21
 */

public class DateDialog extends BaseDialog {

    private Context context;
    private TimePickerView pvTime;
    private OnDateSelectListener onDateSelectListener;
    private String title;
    private TimePickerView.Type type = TimePickerView.Type.YEAR_MONTH_DAY;
    private Calendar startDate;
    private Calendar endDate;

    public DateDialog(Context context) {
        this.context = context;
    }

    public void showPickerView() {
        Calendar selectedDate = Calendar.getInstance();
        //时间选择器
        TimePickerView.Builder builder = new TimePickerView.Builder(context, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                if (onDateSelectListener != null) {
                    onDateSelectListener.onDateSelect(date);
                }
            }
        })
                /*.setType(TimePickerView.Type.ALL)//default is all
                .setCancelText("Cancel")
                .setSubmitText("Sure")
                .setContentSize(18)
                .setTitleSize(20)
                .isCyclic(true)// default is false
                .setTitleColor(Color.BLACK)
               /*.setDividerColor(Color.WHITE)//设置分割线的颜色
                .setTextColorCenter(Color.LTGRAY)//设置选中项的颜色
                .setLineSpacingMultiplier(1.6f)//设置两横线之间的间隔倍数
                .setTitleBgColor(Color.DKGRAY)//标题背景颜色 Night mode
                .setBgColor(Color.BLACK)//滚轮背景颜色 Night mode
                .setSubmitColor(Color.WHITE)
                .setCancelColor(Color.WHITE)*/
               /* .gravity(Gravity.RIGHT)// default is center*/
                .setTitleText(title)
                .setDividerColor(context.getResources().getColor(R.color.line_color))
                .setTextColorCenter(context.getResources().getColor(R.color.common_black_color_3)) //设置选中项文字颜色
                .setCancelColor(context.getResources().getColor(R.color.common_black_color_6))
                .setContentSize(16)
                .setOutSideCancelable(false)// default is true
                .setType(type)
                .setLabel("年", "月", "日", "", "", "") // 设置空字符串以隐藏单位提示   hide label
                .setDate(selectedDate)
                .setRangDate(startDate, endDate);
        if (startDate != null && endDate != null) {
            builder.setRangDate(startDate, endDate);
        }
        pvTime = builder.build();
        View currentFocus = ((Activity) context).getCurrentFocus();
        if (null != currentFocus) {
            ((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE))
                    .hideSoftInputFromWindow(currentFocus.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
        pvTime.show();
    }

    public void setStartDate(int year, int month, int day) {
        startDate = Calendar.getInstance();
        startDate.set(Calendar.YEAR, year);
        startDate.set(Calendar.MONTH, month - 1);
        startDate.set(Calendar.DAY_OF_MONTH, day);
    }

    public void setEndDate(int year, int month, int day) {
        endDate = Calendar.getInstance();
        endDate.set(Calendar.YEAR, year);
        endDate.set(Calendar.MONTH, month - 1);
        endDate.set(Calendar.DAY_OF_MONTH, day);
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setType(TimePickerView.Type type) {
        this.type = type;
    }

    public void setOnDateSelectListener(OnDateSelectListener onDateSelectListener) {
        this.onDateSelectListener = onDateSelectListener;
    }

    public interface OnDateSelectListener {
        void onDateSelect(Date date);
    }
}
